package com.qf.exam01.controller;

import com.qf.exam01.utils.IP;
import com.qf.exam01.utils.IPUtils;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.*;

@RestController
@RequestMapping("/ip")
public class IPContoller {


    /**
     * 该接口生成指定数量的IP地址，并写入文本文件中
     * 文本文件的路径在yml文件中配置，key为 qfsy.ip.file01
     * @return
     */
    @GetMapping("/generate/{number}")
    public List<String> generateIP(@PathVariable("number") Integer number) throws IOException {

        List<String> list = new ArrayList<>();
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:/2204/ip/ip01.txt",true));
        for(int i = 0;i<number;i++) {
            IP ip = IPUtils.randomIP(192, 168, 10);
            System.out.println(ip);
            String s = String.valueOf(ip);
            bufferedWriter.write(s);
            bufferedWriter.newLine();
            list.add(s);

        }
            bufferedWriter.close();
            return list;

    }

    /**
     * 查询上次生成的随机IP地址
     * @return
     */
    @GetMapping("/query")
    public List<String> queryLast() throws Exception {
        List<String> iplist = new ArrayList<>();
        String line = null;
        BufferedReader bufferedReader = new BufferedReader(new FileReader("D:/2204/ip/ip01.txt"));
        while ((line=bufferedReader.readLine())!=null){
            iplist.add(line);
        }
        for (String s : iplist) {
            System.out.println(s);
        }
        bufferedReader.close();
        return iplist;
    }


    /**
     * IP地址去重和统计
     * 1. 把不重复的IP地址写入一个新的文本文件中，文件配置在yml中，key为 qfsy.ip.file01
     * 2. 并在接口中返回
     * - 不重复的IP
     * - IP出现的次数
     * - 重复次数最多的5个IP
     * @return
     */
    @GetMapping("/s")
    public HashMap statistic() throws IOException {
        HashMap<String,Integer> map = new HashMap<>();
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:/2204/ip/ip02.txt"));
        List<String> list = new ArrayList<>();
        String line = null;
        BufferedReader bufferedReader = new BufferedReader(new FileReader("D:/2204/ip/ip01.txt"));
        while ((line=bufferedReader.readLine())!=null){
            list.add(line);
        }
        for (String s : list) {
            map.put(s,map.get(s)==null?1:map.get(s)+1);
        }

        for (String s : map.keySet()) {
            bufferedWriter.write(s+":"+map.get(s));
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();
        List<Map.Entry<String,Integer>> objects = new ArrayList<>(map.size());
        objects.addAll(map.entrySet());
        Collections.sort(objects, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue()-o1.getValue();
            }
        });
        for (Map.Entry<String, Integer> object : objects) {
                System.out.println(object);
        }
        System.out.println("重复次数最多的5个IP:");
        for (int i = 0; i < 5; i++) {
            System.out.println(objects.get(i));
        }
        return map;
    }
}
